package com.qnbc.mystery.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.AddressMapper;
import com.qnbc.mystery.pojo.domain.Address;
import com.qnbc.mystery.service.IAddressService;

/**
 * 地址Service业务层处理
 *
 * @author guzj
 * @date 2023-12-27
 */
@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询地址
     *
     * @param id 地址主键
     * @return 地址
     */
    @Override
    public Address selectAddressById(Long id) {
        return addressMapper.selectAddressById(id);
    }

    /**
     * 查询地址列表
     *
     * @param address 地址
     * @return 地址
     */
    @Override
    public List<Address> selectAddressList(Address address) {
        return addressMapper.selectList(new LambdaQueryWrapper<Address>());
    }

    /**
     * 新增地址
     *
     * @param address 地址
     * @return 结果
     */
    @Override
    public int insertAddress(Address address) {
        if (null != address.getDefaultNot()) {
            //获取之前是否有默认
            LambdaQueryWrapper<Address> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Address::getDefaultNot, "1");
            Address address1 = baseMapper.selectOne(lambdaQueryWrapper);

            if (null != address1) {
                log.info("新增地址时，设置新的默认地址");
                address1.setDefaultNot("2");
                baseMapper.updateById(address1);
            }
        }
        return baseMapper.insert(address);
    }

    /**
     * 修改地址
     *
     * @param address 地址
     * @return 结果
     */
    @Override
    public int updateAddress(Address address) {
        User currentUser = TokenUtils.getCurrentUser();
        address.setUpdatedBy(currentUser.getId().toString());
        address.setUpdatedTime(new Date());
        return addressMapper.updateAddress(address);
    }

    /**
     * 批量删除地址
     *
     * @param ids 需要删除的地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Long[] ids) {
        return addressMapper.deleteAddressByIds(ids);
    }

    /**
     * 删除地址信息
     *
     * @param id 地址主键
     * @return 结果
     */
    @Override
    public int deleteAddressById(Long id) {
        return addressMapper.deleteAddressById(id);
    }

    @Override
    public int setDefault(Long id) {
        LambdaQueryWrapper<Address> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Address::getDefaultNot, "1");
        Address address1 = baseMapper.selectOne(lambdaQueryWrapper);
        if (null != address1) {
            log.info("修改默认地址，设置新的默认地址");
            address1.setDefaultNot("2");
            baseMapper.updateById(address1);
        }
        Address address = new Address();
        address.setId(id);
        address.setDefaultNot("1");

        return addressMapper.updateById(address);
    }
}
